package com.example.afinal.Aboutme;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.afinal.Db.MyHelper;
import com.example.afinal.R;

public class RegisterActivity extends AppCompatActivity {
    private EditText etAccount;
    private EditText etPwd;
    private Button btnRegister;
    private MyHelper myHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //zhuangtai();
        initView();
        myHelper = new MyHelper(this);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name, pwd;
                SQLiteDatabase db;
                ContentValues values;
                boolean result = false;
                name = etAccount.getText().toString();
                pwd = etPwd.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        db = myHelper.getReadableDatabase();
                        Cursor cursor = db.rawQuery("select * from users where name=?", new String[]{name});
                        result = cursor.moveToNext();
                        cursor.close();
                        db.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (result) {
                            Toast.makeText(getApplicationContext(), "账号已存在，请重新注册！", Toast.LENGTH_SHORT).show();
                        } else {
                            //获取可读写的SQLLiteDataBase对象
                            db = myHelper.getWritableDatabase();
                            //创建ContenValues对象
                            values = new ContentValues();
                            values.put("name", name);
                            values.put("pwd", pwd);
                            db.insert("users", null, values);
                            Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                            db.close();
                            finish();
                        }
                    }

                }
            }
        });
    }

    private void initView() {
        etAccount = (EditText) findViewById(R.id.et_account);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        btnRegister = (Button) findViewById(R.id.btn_register);
    }

    /**
     * 隐藏状态栏
     */
    private void zhuangtai() {
        //View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        //decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

}
